package com.youlu.campus.activity.service.impl;

import com.youlu.campus.activity.entity.ActivityLotteryConfigPrize;
import com.youlu.campus.activity.repositories.ActivityLotteryConfigPrizeRepository;
import com.youlu.campus.activity.service.ActivityLotteryConfigPrizeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: ActivityLotteryConfigPrizeServiceImpl
 * @Date 2025/8/4
 * @module ActivityLotteryConfigPrizeServiceImpl
 * @author: su
 */
@Slf4j
@Service
public class ActivityLotteryConfigPrizeServiceImpl implements ActivityLotteryConfigPrizeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ActivityLotteryConfigPrizeRepository repository;

    @Override
    public List<ActivityLotteryConfigPrize> findByLotteryId(String lotteryId) {
        return repository.findByLotteryId(lotteryId);
    }

    @Override
    public void batchSaveByLotteryId(String lotteryId, List<ActivityLotteryConfigPrize> prizes) {
        List<ActivityLotteryConfigPrize> oldPrizes = repository.findByLotteryId(lotteryId);
        if (CollectionUtils.isEmpty(oldPrizes)) {
            LocalDateTime now = LocalDateTime.now();
            for (ActivityLotteryConfigPrize prize : prizes) {
                prize.setCreateTime(now);
                prize.setRemainingCount(prize.getTotalCount());//剩余数量
            }
        } else {
            repository.deleteByLotteryId(lotteryId);
            Map<String, ActivityLotteryConfigPrize> oldMap = oldPrizes.stream().collect(Collectors.toMap(ActivityLotteryConfigPrize::getId, o -> o));
            LocalDateTime now = LocalDateTime.now();
            for (ActivityLotteryConfigPrize prize : prizes) {
                //剩余数量
                if (StringUtils.isNotBlank(prize.getId())) {
                    ActivityLotteryConfigPrize old = oldMap.get(prize.getId());
                    if (Objects.nonNull(old)) {
                        prize.setRemainingCount(old.getRemainingCount());
                    }
                }
                prize.setCreateTime(now);
            }
        }
        repository.saveAll(prizes);
    }

    @Override
    public void updateRemainingCountById(String configPrizeId, long remainingCount) {
        Query query = new Query(Criteria.where("id").is(configPrizeId));

        Update update = new Update();
        update.set("remainingCount", remainingCount);
        mongoTemplate.updateMulti(query, update, ActivityLotteryConfigPrize.class);
    }
}
